
// @notice 依赖 CP_MouseMove



//-----------------------------------------------------------------------------
//SWDA_Sprite_Button
//-----------------------------------------------------------------------------
let SWDA_Sprite_Button =  {};

//SWDA
SWDA_Sprite_Button.spriteButtonUpdate = Sprite_Button.prototype.update;
Sprite_Button.prototype.update = function () {
    SWDA_Sprite_Button.spriteButtonUpdate.call(this);
    this.processMouseMove();
};


//SWDA
Sprite_Button.prototype.processMouseMove = function () {
    if (this.isActive() && this.isMovedInsideFrame() && (TouchInput.movingdate > Input.date)) {
        if (this._onFrame) {
            this.onFrame();
        }

    }
};

//-----------------------------------------------------------------------------
// Sprite_Button
//
// The sprite for displaying a button.
// 补充定义：按下（通常window中键盘控制时刷新按钮）
// hot：按下
// cold：未触碰
// on：触摸/光标选中

Sprite_Button.prototype.hotFrame = function () {
    const frame = this._hotFrame;
    this.setFrame(frame.x, frame.y, frame.width, frame.height);
};


// 补充定义：On（鼠标移动到上面时变亮）
Sprite_Button.prototype.onFrame = function () {
    if (!this._touching) {
        const frame = this._onFrame;
        this.setFrame(frame.x, frame.y, frame.width, frame.height);
    }
};

Sprite_Button.prototype.setOnFrame = function (x, y, width, height) {
    this._onFrame = new Rectangle(x, y, width, height);
};


Sprite_Button.prototype.isMovedInsideFrame = function () {
    const x = this.canvasToLocalX(TouchInput.realX);
    const y = this.canvasToLocalY(TouchInput.realY);
    return x >= 0 && y >= 0 && x < this.width && y < this.height;

};


Sprite_Button.prototype.updateFrame = function () {
    let frame;
    if (this._touching && this.isActive()) {
        frame = this._hotFrame;
    } else {
        frame = this._coldFrame;
    }

    if (frame) {
        this.setFrame(frame.x, frame.y, frame.width, frame.height);
    }
    if (this.isEnabled() === false) {
        this.setColorTone([-50, -50, -50, 220]);
    } else {
        this.setColorTone([0, 0, 0, 0]);
    }

};

Sprite_Button.prototype.isActive = function () {
    let node = this;
    while (node) {
        if (!node.visible) {
            return false;
        }
        node = node.parent;
    }
    if (!this.parent.isOpenAndActive()) {
        return false;
    }
    return true;
};

Sprite_Button.prototype.isEnabled = function () {
    return this.enabled;
};

//------------------------------
//         TouchInput
//------------------------------

Object.defineProperty(TouchInput, 'movingdate', {//swda
    get: function () {
        return this._movingdate;
    },
    configurable: true
});

SWDA_Sprite_Button.TOUCH_CLEAR = TouchInput.clear;
TouchInput.clear = function () {
    SWDA_Sprite_Button.TOUCH_CLEAR.call(this);
    this._movingdate = 0;	//记录移动鼠标的时间
};




SWDA_Sprite_Button.TOUCH_UPDATE_MOVING = TouchInput.updateMoving;
TouchInput.updateMoving = function () {
    SWDA_Sprite_Button.TOUCH_UPDATE_MOVING.call(this);
    this._moving = this._oldX !== this.realX || this._oldY !== this.realY;
    if (this._moving) { this._movingdate = Date.now(); }	//记录移动鼠标的时间

};